package com.stellar.circle.basic.service.impl;

import com.stellar.circle.basic.dao.SCommentLikeDao;
import com.stellar.circle.basic.entity.SCommentLike;
import com.stellar.circle.basic.service.SCommentLikeService;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 评论点赞表(SCommentLike)服务实现
 *
 * @author Jqchan
 * @since 2025-05-19 14:53:10
 */
@Service
public class SCommentLikeServiceImpl implements SCommentLikeService {

    @Resource
    private SCommentLikeDao sCommentLikeDao;

    @Override
    public SCommentLike queryById(Long id) {
        return sCommentLikeDao.queryById(id);
    }

    @Override
    public SCommentLike queryByCondition(SCommentLike condition) {
        return sCommentLikeDao.queryByCondition(condition);
    }

    @Override
    public List<SCommentLike> queryAllByCondition(SCommentLike condition) {
        return sCommentLikeDao.queryAllByCondition(condition);
    }

    @Override
    public List<SCommentLike> page(SCommentLike condition, int page, int pageSize) {
        return sCommentLikeDao.page(condition, page, pageSize);
    }

    @Override
    public int insert(SCommentLike entity) {
        return sCommentLikeDao.insert(entity);
    }

    @Override
    public int update(SCommentLike entity) {
        return sCommentLikeDao.update(entity);
    }

    @Override
    public int deleteById(Long id) {
        return sCommentLikeDao.deleteById(id);
    }

    @Override
    public Long count(SCommentLike condition) {
        return sCommentLikeDao.count(condition);
    }

    @Override
    public Map<Long, Boolean> batchGetLikeStatus(Set<Long> commentIds, Long userId) {
        if (CollectionUtils.isEmpty(commentIds) || userId == null) {
            return Collections.emptyMap();
        }

        List<SCommentLike> likes = sCommentLikeDao.selectByCommentIdsAndUser(
                commentIds, userId);

        return commentIds.stream()
                .collect(Collectors.toMap(
                        id -> id,
                        id -> likes.stream().anyMatch(like -> like.getCommentId().equals(id))
                ));
    }

    @Override
    public Boolean checkLikeStatus(Long id, Long currentUserId) {
        if (id != null && currentUserId != null) {
            SCommentLike condition = new SCommentLike();
            condition.setCommentId(id);
            condition.setUserId(currentUserId);
            condition.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
            return sCommentLikeDao.queryByCondition(condition) != null;
        }
        return false;
    }
}
